/*
 * Copyright (c) 2007 Compl Yue Still, All Rights Reserved.
 */

package oex.ui
{
	import flash.display.*;
	import flash.events.*;
	import flash.ui.*;
	
	import mx.containers.*;
	import mx.controls.*;
	import mx.core.*;

	public class ObjectShape extends UIComponent
	{
		
		protected var _color:uint;
		
		public function ObjectShape(color:uint=0)
		{
			if(color == 0) color = Math.random()*0xFFFFFF;
			_color = color;
			
			this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
		}
		
		public function redrawShape(alpha:Number):void
		{
			// this class is abstract, draw nothing here
		}
		
		protected function populateContextMenu(cm:ContextMenu):void
		{
			var mi:ContextMenuItem;
			
			mi = new ContextMenuItem("Rotate Left");
			mi.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, 
				function(event:ContextMenuEvent):void
			{
				var shape:ObjectShape = ObjectShape(event.contextMenuOwner);
				shape.rotation -= 30;
				var space:FreeSpace = FreeSpace(shape.parent.parent);
				space.shapeRotated(shape);
			});
			cm.customItems.push(mi);

			mi = new ContextMenuItem("Rotate Right");
			mi.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, 
				function(event:ContextMenuEvent):void
			{
				var shape:ObjectShape = ObjectShape(event.contextMenuOwner);
				shape.rotation += 30;
				var space:FreeSpace = FreeSpace(shape.parent.parent);
				space.shapeRotated(shape);
			});
			cm.customItems.push(mi);
			
			mi = new ContextMenuItem("Enlarge");
			mi.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, 
				function(event:ContextMenuEvent):void
			{
				var shape:ObjectShape = ObjectShape(event.contextMenuOwner);
				shape.scaleX *= 1.2;
				shape.scaleY *= 1.2;
				var space:FreeSpace = FreeSpace(shape.parent.parent);
				space.shapeScaled(shape);
			});
			cm.customItems.push(mi);
			
			mi = new ContextMenuItem("Shrink");
			mi.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, 
				function(event:ContextMenuEvent):void
			{
				var shape:ObjectShape = ObjectShape(event.contextMenuOwner);
				shape.scaleX /= 1.2;
				shape.scaleY /= 1.2;
				var space:FreeSpace = FreeSpace(shape.parent.parent);
				space.shapeScaled(shape);
			});
			cm.customItems.push(mi);
			
			mi = new ContextMenuItem("Remove");
			mi.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,
				function (event:ContextMenuEvent):void
			{
				var shape:ObjectShape = ObjectShape(event.contextMenuOwner);
				var space:FreeSpace = FreeSpace(shape.parent.parent);
				space.removeShape(shape);
			});
			cm.customItems.push(mi);

		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			if(!this.contextMenu)
			{
				var cm:ContextMenu = new ContextMenu();
				cm.hideBuiltInItems();
				
				this.populateContextMenu(cm);
				
				this.contextMenu = cm;
			}
		}

		internal function mouseDown(event:MouseEvent):void
		{
			this.startDrag();
		}
		internal function mouseReleased(event:MouseEvent):void
		{
			this.stopDrag();

			var shape:ObjectShape = ObjectShape(event.target);
			var space:FreeSpace = FreeSpace(shape.parent.parent);
			space.shapeMoved(shape);
		}
		
		
		private var _oid:int = 0;
		
		public function get oid():int
		{
			return _oid;
		}
		
		public function set oid(oid:int):void
		{
			_oid = oid;
		}
		
		public function get color():uint
		{
			return _color;
		}
		
		public function set color(color:uint):void
		{
			_color = color;
			redrawShape(0.8);
		}
		
	}
}
